﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Galaktika.BI.Runtime;
using Galaktika.BI.Silverlight.Services;
using Ranet.AgOlap.Controls.MemberChoice.ClientServer;
using Ranet.AgOlap.Controls.General.ClientServer;
using Ranet.Olap.Core.Providers.ClientServer;
using Ranet.Olap.Core.Storage;

namespace Galaktika.BI.Silverlight.Commands
{
    public static class CommandBuilder
    {
        private static InvokeSchema CreateGetDataCommand(
            string connectionString,
            string query,
            string sessionId,
            ConnectionType connectionType,
            string contexId,
            string dataSourceId)
        {
            InvokeSchema schema = new InvokeSchema(CommandId.GetDataCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ConnectionString, connectionString));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.Query, query));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.SessionId, sessionId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ConnectionType, connectionType.ToString()));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contexId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.QueryId, Guid.NewGuid().ToString()));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));

            return schema;
        }

        private static InvokeSchema CreateGetDbDataCommand(
            string connectionString,
            string query,
            ConnectionType connectionType,
            string contextId,
            string dataSourceId)
        {
            return CreateGetDataCommand(connectionString, query, null, connectionType, contextId, dataSourceId);
        }

        public static InvokeSchema CreateGetOleDbCommand(
            string connectionString,
            string query,
            string contextId,
            string dataSourceId)
        {
            return CreateGetDbDataCommand(connectionString, query, ConnectionType.OleDb, contextId, dataSourceId);
        }

        public static InvokeSchema CreateGetOdbcCommand(
            string connectionString,
            string query,
            string contextId,
            string dataSourceId)
        {
            return CreateGetDbDataCommand(connectionString, query, ConnectionType.Odbc, contextId, dataSourceId);
        }

        public static InvokeSchema CreateGetSqlCommand(
            string connectionString,
            string query,
            string contextId,
            string dataSourceId)
        {
            return CreateGetDbDataCommand(connectionString, query, ConnectionType.SqlServer, contextId, dataSourceId);
        }

        public static InvokeSchema CreateAdomdCommand(
            string connectionString,
            string query,
            string sessionId,
            string contexId,
            string dataSourceId)
        {
            return CreateGetDataCommand(connectionString, query, sessionId, ConnectionType.Olap, contexId, dataSourceId);
        }


        public static InvokeSchema CreateModifyVariablesSchema(string contextId, IEnumerable<NameValueEntry> entries)
        {
            InvokeSchema schema = new InvokeSchema(CommandId.ModifyVariablesId);
            schema.Args.Add(new InvokeArg(
                KnownInvokeArgs.ContextId,
                contextId));
            NameValueDictionary dict = new NameValueDictionary(entries);
            schema.Args.Add(new InvokeArg(
                KnownInvokeArgs.NameValueCollection,
                dict.ToXml()));

            return schema;
        }

        public static InvokeSchema CreateCommitTransactionSchema(
            string contextId, 
            string connectionName)
        {
            InvokeSchema schema = new InvokeSchema(CommandId.CommitTransactionCommandId);
            schema.Args.Add(new InvokeArg(
                KnownInvokeArgs.ContextId,
                contextId));
            schema.Args.Add(new InvokeArg(
                KnownInvokeArgs.ConnectionName,
                connectionName));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.QueryId, Guid.NewGuid().ToString()));
            return schema;
        }

        public static InvokeSchema CreateRollbackTransactionSchema(
            string contextId, 
            string connectionName)
        {
            InvokeSchema schema = new InvokeSchema(CommandId.RollbackTransactionCommandId);
            schema.Args.Add(new InvokeArg(
                KnownInvokeArgs.ContextId,
                contextId));
            schema.Args.Add(new InvokeArg(
                KnownInvokeArgs.ConnectionName,
                connectionName));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.QueryId, Guid.NewGuid().ToString()));
            return schema;
        }

        public static InvokeSchema CreateGetMetadataSchema(string contextId, String connection, MetadataQuery args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.GetMetadataCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ConnectionId, connection));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.MetadataQueryInfo, XmlUtility.Obj2XmlStr(args, Common.Namespace)));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.QueryId, Guid.NewGuid().ToString()));

            return schema;
        }

        public static InvokeSchema CreateStorageActionSchema(string contextId, StorageActionArgs args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.StorageActionCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));

            schema.Args.Add(new InvokeArg(KnownInvokeArgs.StorageAction, XmlUtility.Obj2XmlStr(args, Common.Namespace)));
            return schema;
        }

        public static InvokeSchema CreateMemberChoiceQuerySchema(string contextId, String connection, MemberChoiceQuery args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.MemberChoiceCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ConnectionId, connection));

            schema.Args.Add(new InvokeArg(KnownInvokeArgs.MemberChoiceQueryInfo, XmlUtility.Obj2XmlStr(args, Common.Namespace)));
            return schema;
        }

        public static InvokeSchema CreateGetPivotDataSchema(string contextId, String dataSourceId, PivotInitializeArgs args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.GetPivotDataCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.GetPivotDataCommand, XmlUtility.Obj2XmlStr(args, Common.Namespace)));

            return schema;
        }

        public static InvokeSchema CreateChangeDataSourceSchema(string contextId, String dataSourceId, PivotInitializeArgs args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.ChangeDataSourceCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ChangeDataSource, XmlUtility.Obj2XmlStr(args, Common.Namespace)));

            return schema;
        }

        public static InvokeSchema CreateExecuteQuerySchema(string contextId, String dataSourceId, String connectionId, MdxQueryArgs args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.ExecuteQueryCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ConnectionId, connectionId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ExecuteQueryCommand, XmlUtility.Obj2XmlStr(args, Common.Namespace)));

            return schema;
        }

        public static InvokeSchema CreateRunServiceCommandSchema(string contextId, String dataSourceId, ServiceCommandArgs args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.ServiceCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.PivotGridServiceCommand, XmlUtility.Obj2XmlStr(args, Common.Namespace)));
            
            return schema;
        }

        public static InvokeSchema CreateDrillDownSchema(string contextId, String dataSourceId, PerformMemberActionArgs olap_args)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.DrillDownCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.MemberAction, XmlUtility.Obj2XmlStr(olap_args, Common.Namespace)));

            return schema;
        }

        public static InvokeSchema CreateUpdateCubeSchema(string contextId, string dataSourceId, UpdateCubeArgs olap_args, string sessionId)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.UpdateCubeCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.SessionId, sessionId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.UpdateCube, XmlUtility.Obj2XmlStr(olap_args, Common.Namespace)));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.QueryId, Guid.NewGuid().ToString()));

            return schema;
        }

        public static InvokeSchema CreateToolBarInfoSchema(string contextId, String dataSourceId)
        {
            InvokeSchema schema = new InvokeSchema(Commands.CommandId.ToolBarInfoCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ContextId, contextId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.DataSourceId, dataSourceId));

            return schema;
        }

        public static InvokeSchema CreateGetActualityDateSchema(
            string connectionString, 
            string sessionId,
            string cubeName)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                throw new ArgumentNullException(connectionString);
            }
            if (string.IsNullOrEmpty(cubeName))
            {
                throw new ArgumentNullException(cubeName);
            }

            var schema = new InvokeSchema(Commands.CommandId.GetActualityDateCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.ConnectionString, connectionString));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.SessionId, sessionId));
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.CubeName, cubeName));

            return schema;
        }

        public static InvokeSchema CreateCancelQueryCommand(string queryId)
        {
            if (string.IsNullOrEmpty(queryId))
            {
                throw new ArgumentNullException("queryId");
            }

            var schema = new InvokeSchema(Commands.CommandId.CancelQueryCommandId);
            schema.Args.Add(new InvokeArg(KnownInvokeArgs.QueryId, queryId));
            return schema;
        }
    }
}
